/**
 * 
 */
package com.gosophia.govDoc.action.transientDoc;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.dataDictionary.ws.DataDictionaryWebService;
import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 功能：
 * 
 * @author zhangdongsheng
 * @since 2011-2-9 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Controller("TransientDocBackAction")
@Scope("prototype")
@SuppressWarnings("serial")
public class TransientDocBackAction extends ActionSupport {
    private String stepNo;// 步骤编号
    private Long jobId;// 任务id
    private TransientDocumentDTO transientDocumentDto;
    private List<DataValueDTO> dataValueList;
    private String fromFlag;

    private MissionDTO missionDto;

    @Autowired
    private TransientDocumentWebService transientDocumentWebService;
    @Autowired
    private DataDictionaryWebService dataDictionaryWebService;
    @Autowired
    private TaskManagementWebService taskManagementWebService;

    private final Logger logger = Logger.getLogger(this.getClass());

    @Override
    public String execute() throws Exception {

        // 得到当前用户
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
                .getUserInfoId();
        dataValueList = dataDictionaryWebService
                .findEnabledDataValueByEntryName("zhongyaochengdu");// 任务重要程度
        // 如果jobId 不为空，判定从任务系统来，否则判定从业务系统来
        stepNo = prepareDataForBackAction(creatorId, jobId, stepNo);
        return stepNo;
    }

    private String prepareDataForBackAction(Long creatorId, Long jobId,
            String stepNo) throws BusinessException {
        if ("first".equals(stepNo)) {
            dataValueList = dataDictionaryWebService
                    .findEnabledDataValueByEntryName("zhongyaochengdu");// 任务重要程度
            if (jobId != null) {// 从任务来的 第一步
                JobDTO jobDTO = taskManagementWebService
                        .findJobDetailByJobId(jobId);
                missionDto = jobDTO.getMissionDTO();
                stepNo = stepNo + "_task";
            } else {// 从业务来第一步
                transientDocumentDto = transientDocumentWebService
                        .findTranDocByCreatorAndJobId(creatorId, jobId);
            }
        } else {// 其他的文档起草上一步 查询 数据
            transientDocumentDto = transientDocumentWebService
                    .findTranDocByCreatorAndJobId(creatorId, jobId);
        }
        return stepNo;
    }

    public String getStepNo() {
        return stepNo;
    }

    public void setStepNo(String stepNo) {
        this.stepNo = stepNo;
    }

    public Long getJobId() {
        return jobId;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    public TransientDocumentDTO getTransientDocumentDto() {
        return transientDocumentDto;
    }

    public void setTransientDocumentDto(
            TransientDocumentDTO transientDocumentDto) {
        this.transientDocumentDto = transientDocumentDto;
    }

    public List<DataValueDTO> getDataValueList() {
        return dataValueList;
    }

    public void setDataValueList(List<DataValueDTO> dataValueList) {
        this.dataValueList = dataValueList;
    }

    public String getFromFlag() {
        return fromFlag;
    }

    public void setFromFlag(String fromFlag) {
        this.fromFlag = fromFlag;
    }

    public MissionDTO getMissionDto() {
        return missionDto;
    }

    public void setMissionDto(MissionDTO missionDto) {
        this.missionDto = missionDto;
    }

}
